package ascript.utilities
{
	//#########################
	// Header Includes
	//#########################
	import starling.display.Sprite;
	import starling.display.MovieClip;
	import starling.animation.Juggler;
	import starling.core.Starling;

	//#########################
	// GraphicsAnimated Class
	//#########################
	public class GraphicsMovie extends Sprite
	{
		//=========================
		// Variables
		//=========================
		private var graphicAnimationFPS:uint;
		private var graphicAnimation:MovieClip;
		private var graphicAnimations:Vector.<MovieClip> = new Vector.<MovieClip>;
		private var graphicAnimationsIndex:int;
		
		//=========================
		// Constructors
		//=========================	
		public function GraphicsMovie( tMovies:Vector.<DataMovie>, tIsVisible:Boolean, tLocation:PointSimple, tPivot:PointSimple, tIndex:uint, tIsLooping:Boolean, tIsPaused:Boolean ):void
		{
			var tempListLength:uint = tMovies.length;
			for ( var i:uint = 0; i < tempListLength; i++ )
			{
				graphicAnimations.push( new MovieClip( tMovies[i].textures, tMovies[i].fps ) );
			}
			
			graphicAnimationsIndex = tIndex;
			graphicAnimation = graphicAnimations[ tIndex ];
			
			x = tLocation.x;
			y = tLocation.y;
			pivotX = tPivot.x;
			pivotY = tPivot.y;
			visible = tIsVisible;
			
			setGraphic( tIndex, tIsLooping );
			controlGraphic( 0, tIsPaused );
		}		
		
		//=========================
		// Members
		//=========================
		public function controlGraphic( tFPS:uint = 0, tIsPaused:Boolean = false, tIsStopped:Boolean = false ):void
		{			
			if ( tIsStopped )
			{
				graphicAnimation.stop();
			}
			else
			{
				if ( tIsPaused )
				{
					graphicAnimation.pause();
				}
				else
				{
					graphicAnimation.play();
				}
			}
			
			if ( tFPS > 0 )
			{
				graphicAnimation.fps = tFPS;
			}
		}
		
		public function setGraphic( tIndex:uint, tIsLooping:Boolean = true, tSmoothing:String = "none" ):void
		{
			if ( tIndex >= 0 && tIndex < graphicAnimations.length && tIndex != graphicAnimationsIndex )
			{
				graphicAnimationsIndex = tIndex;
				removeChild( graphicAnimation );
				( Starling.juggler ).remove( graphicAnimation );
				
				graphicAnimation = graphicAnimations[ tIndex ];				
				( Starling.juggler ).add( graphicAnimation );
				addChild( graphicAnimation );
				
				graphicAnimation.loop = tIsLooping;
				graphicAnimation.smoothing = tSmoothing;
			}
		}
		
		public function getIndexGraphic():int
		{
			return graphicAnimationsIndex;
		}
		
		public function getFPS( tIsDefault:Boolean = false ):int
		{
			if ( tIsDefault )
			{
				return graphicAnimationFPS;
			}
			return graphicAnimation.fps;
		}
	}
}